package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 根据id查询套餐
     * @param id
     */
    @Override
    public SetmealVO selectById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        //属性拷贝
        BeanUtils.copyProperties(setmeal,setmealVO);

        //根据id查询套餐菜品
        setmealVO.setSetmealDishes(setmealDishMapper.selectDishById(id));

        return setmealVO;
    }

    /**
     * 新建套餐
     * @param setmealDTO
     */
    @Transactional
    public void newSetmeal(SetmealDTO setmealDTO) {

        //一般数据插入
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.insert(setmeal);

        //获取生成的主键
        Long setmealId = setmeal.getId();

        //套餐包含的菜品插入setmealDishes 有n个菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if(setmealDishes != null && !setmealDishes.isEmpty()){
            //将套餐的主键插入到套餐菜品关系实体里
            setmealDishes.forEach(setmealDish->{
                setmealDish.setSetmealId(setmealId);
            });
        }

        //保存套餐和菜品的关联关系//?
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //底层是select * from setmeal limit 0,10
        //开始分页使用pageHelper插件
        //?
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealDishMapper.pageQuery(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    /**
     * 批量删除套餐及其关系表
     * @param ids
     */
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        //删除套餐
        //判断套餐是否在售
        for(long id :ids){
            Setmeal setmeal = setmealMapper.getSetmealById(id);
            if(setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.delete(ids);

        //删除关联表
        setmealDishMapper.delete(ids);
    }

    /**
     * 修改套餐 包含了删除或增加菜品
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //属性拷贝
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.update(setmeal);

        //获得菜品主键
        Long setmealId = setmeal.getId();

        //菜品的删除或添加
        //删除所有的套餐菜品
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        //新增菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //将菜品的id插入
        setmealDishes.forEach(setmealDish->{
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insertBatch(setmealDishes);



    }

    /**
     * 启售停售套餐
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, long id) {
        //根据id查找setmeal
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        //根据套餐id查询菜品的状态
        List<SetmealDish> setmealDishes =  setmealDishMapper.selectDishById(setmeal.getId());
        //判断套餐里是否有未起售菜品如果有报错
        setmealDishes.forEach(setmealDish ->{
             if(StatusConstant.DISABLE== dishMapper.getById(setmealDish.getDishId()).getStatus()){
                 throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
             }
        });
        //改变状态
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
